//
//  StateViewController.m
//  safeLady
//
//  Created by Justsoso on 2017/7/3.
//  Copyright © 2017年 xmy. All rights reserved.
//

#import "StateViewController.h"
#import "StateViewModel.h"
#import "LustDetailsViewController.h"
//#import "UIButton+WebCache.h"

#define kGradeViewBackground  [UIColor colorWithHex:0xE3E3EA alpha:1]

@interface StateViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *left_menu;

@property (nonatomic, strong) UIView *gradeView;            // 分数模块View
@property (nonatomic, strong) UIView *physiologyView;       // 生理模块View
@property (nonatomic, strong) UIView *healthView;           // 健康模块View
@property (nonatomic, strong) UIView *lustView;             // 情欲模块View

@property (nonatomic, strong) UIButton *dateButton;         // 经期日历按钮


@property (nonatomic, strong) CAShapeLayer *bigRoundLine;         // 大圆环
@property (nonatomic, strong) CAShapeLayer *bigRoundBackgroundLine;   // 大圆环背景
@property (nonatomic, strong) CAShapeLayer *smallRoundLine;       // 小圆环
@property (nonatomic, strong) CAShapeLayer *smallRoundBackgroundLine;  // 小圆环背景
@property (nonatomic, strong) CAShapeLayer *fan;                       // 扇形

@property (nonatomic, strong) StateViewModel *viewModel;

@property (nonatomic, strong) UIButton *healthButton;       // 生理健康Button
@property (nonatomic, strong) UIButton *lustButton;         // 情欲Button

@property (nonatomic, assign) CGFloat newWidth;
@property (nonatomic, assign) CGFloat newHeight;
@property (nonatomic, assign) CGFloat oldContentOffset;


@end

@implementation StateViewController
@synthesize viewModel = _viewModel;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    //    [self.navigationController.navigationBar setTintColor:kGradeViewBackground];
    
    //    CGRect frame = self.navigationController.navigationBar.frame;
    //    UIView *alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, frame.size.width, frame.size.height+20)];
    //    alphaView.backgroundColor = kGradeViewBackground;
    //    alphaView.userInteractionEnabled = NO;
    //    [self.navigationController.navigationBar insertSubview: alphaView atIndex:0];
    
    
    self.newWidth = 6;
    self.newHeight = 2;
    self.oldContentOffset = 0;
    
    
    /* 添加nav左边按钮 */
    [self setCenterButton];
    /* 界面初始化 */
    [self initView];
    /* 滑动的内容，不设置为0 */
    //    self.scrollView.contentSize = CGSizeMake(self.view.width,560.f);
    /* 是否可以滑动 */
    self.scrollView.scrollEnabled = YES;
    /* 是否开启弹簧效果 */
    self.scrollView.bounces = YES;
    /* 是否展示水平方向的滑动条 */
    self.scrollView.showsHorizontalScrollIndicator = NO;
    /* 是否展示垂直方向的滑动条 */
    self.scrollView.showsVerticalScrollIndicator = NO;
    /* 设置背景颜色 */
    self.scrollView.backgroundColor = [UIColor colorWithHex:0xF7F7F7 alpha:1];
    
    self.scrollView.delegate = self;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [self startAnimation];
}


/**
 界面初始化
 */
- (void)initView {
    
    CGFloat labelTopPadding = H(20.f);
    CGFloat viewTopPadding = H(10.f);
    CGFloat AllLeftPadding = W(15);
    CGFloat allWidth = self.view.frame.size.width - (2 *AllLeftPadding);
    CGFloat gradViewHeight = H(450.f);
    CGFloat physiologyViewHeight = H(100.f);
    CGFloat healthViewHeight = H(180.f);
    CGFloat lustViewHeight = H(220.f);
    CGFloat labelHeight = H(15.f);
    
    CGFloat scrollViewHight = gradViewHeight + physiologyViewHeight + healthViewHeight + lustViewHeight + (3 * labelTopPadding) + (3 * viewTopPadding) + (2 * labelHeight) + H(50);
    /* 滑动的内容，不设置为0 */
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width,scrollViewHight);
    
    
    
    /* 健康模块 */
    self.gradeView = [[UIView alloc] init];
    self.gradeView.backgroundColor = [UIColor gradientFromColor:[UIColor colorWithHex:0xF7F7F7] toColor:[UIColor colorWithHex:0xB1B2C9] withHeight:gradViewHeight + 1];
    [self.scrollView addSubview:self.gradeView];
    [self.gradeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_top);
        make.width.equalTo(self.scrollView.mas_width);
        make.height.equalTo(@(gradViewHeight));
        make.centerX.equalTo(self.scrollView.mas_centerX);
    }];
    
    
    /* 半圆的线 */
    self.bigRoundLine = [CAShapeLayer new];
    self.bigRoundBackgroundLine = [CAShapeLayer new];
    /* 线宽 */
    self.bigRoundLine.lineWidth = 4;
    self.bigRoundBackgroundLine.lineWidth = 2;
    /* 圆环边缘是圆的 */
    self.bigRoundLine.lineCap = kCALineCapRound;
    /* 圆环的颜色 */
    self.bigRoundLine.strokeColor = [UIColor whiteColor].CGColor;
    self.bigRoundBackgroundLine.strokeColor = [UIColor colorWithHex:0xF6F6F8 alpha:1].CGColor;
    /* 背景填充色 */
    self.bigRoundLine.fillColor = [UIColor clearColor].CGColor;
    self.bigRoundLine.shadowOffset = CGSizeMake(0, 2);
    self.bigRoundLine.shadowColor = [UIColor whiteColor].CGColor;
    self.bigRoundLine.shadowRadius = 4;
    self.bigRoundLine.shadowOpacity = 0.9;
    self.bigRoundBackgroundLine.fillColor = [UIColor clearColor].CGColor;
    
    
    /* 按照顺时针方向 */
    BOOL clockWise = true;
    
    
    CGFloat percent = [self.userClient.userStateInfo[@"score"] floatValue];
    
    if (percent >= 100) {
        percent = 3.5f;
    }else if (percent >= 75) {
        percent = (percent - 75) / 5 * 0.1 + 1;
    }else if (percent >= 50) {
        percent = (percent - 50) / 5 * 0.1 + 0.5;
    }else if (percent >= 25) {
        percent = (percent - 25) / 5 * 0.1 + 0;
    }else if (percent >= 0) {
        percent = percent / 5 * 0.1 + 1.5;
    }
    /* 初始化一个路径 */
    CGFloat radius = W(80);
    CGFloat pathX = self.view.centerX;
    CGFloat pathY = 10 + radius + H(200);
    self.bigRoundLine.path = [self getBezierLine:percent];
    
    //    self.arcLine.strokeStart = 0;
    //    self.arcLine.strokeEnd = 1;
    
    
    
    /* 整圆路径 */
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(pathX, pathY) radius:radius startAngle:1.5*M_PI endAngle:3.5*M_PI clockwise:clockWise];
    self.bigRoundBackgroundLine.path = [path1 CGPath];
    
    [self.scrollView.layer addSublayer:self.bigRoundBackgroundLine];
    [self.scrollView.layer addSublayer:self.bigRoundLine];
    
    
    
    
    /* 分值 */
    UILabel *gradeMake = [[UILabel alloc] init];
    gradeMake.font = [UIFont boldSystemFontOfSize:25.f];
    gradeMake.text = [NSString stringWithFormat:@"%@  分",self.userClient.userStateInfo[@"score"]]; // 从本地取健康分数
    gradeMake.textColor = [UIColor whiteColor];
    gradeMake.textAlignment = NSTextAlignmentCenter;
    [self.gradeView addSubview:gradeMake];
    [gradeMake mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.gradeView.mas_centerX);
        make.top.equalTo(@(pathY + H(5)));
        //        make.width.equalTo(@(allWidth));
        make.height.equalTo(@25);
    }];
    
    
    
    
    CGFloat grade = [self.userClient.userStateInfo[@"score"] floatValue];
    UIImageView *faceImageView = [[UIImageView alloc] init];
    UILabel *gradeLabel = [[UILabel alloc] init];
    gradeLabel.font = [UIFont systemFontOfSize:25.f];
    if (grade >= 230) {
        gradeLabel.text = @"优秀";
        [faceImageView setImage:[UIImage imageNamed:@"优秀"]];
    }else if (grade > 196 && grade < 230) {
        gradeLabel.text = @"良好";
        [faceImageView setImage:[UIImage imageNamed:@"良好"]];
    }else if (grade <= 196) {
        gradeLabel.text = @"糟糕";
        [faceImageView setImage:[UIImage imageNamed:@"糟糕"]];
    }
    
    gradeLabel.textColor = [UIColor whiteColor];
    gradeLabel.textAlignment = NSTextAlignmentCenter;
    [self.gradeView addSubview:gradeLabel];
    [gradeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.centerX.equalTo(self.gradeView.mas_centerX);
        make.right.equalTo(gradeMake.mas_right);
        make.top.equalTo(@(pathY - H(5) - 25));
        //        make.width.equalTo(@(allWidth));
        make.height.equalTo(@25);
    }];
    
    [self.gradeView addSubview:faceImageView];
    [faceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(gradeMake.mas_left);
        make.centerY.equalTo(gradeLabel.mas_centerY);
        make.width.height.equalTo(@25);
    }];
    
    
    
    
    /* 时间 */
    NSString *str = self.userClient.userStateInfo[@"date"];
    int month = [[str substringWithRange:NSMakeRange(5, 2)] intValue];
    int day = [[str substringWithRange:NSMakeRange(8, 2)] intValue];
    int hour = [[str substringWithRange:NSMakeRange(11, 2)] intValue];
    int min = [[str substringWithRange:NSMakeRange(14, 2)] intValue];
    
    UILabel *dateLabel = [[UILabel alloc] init];
    dateLabel.font = [UIFont boldSystemFontOfSize:15.f];
    dateLabel.text = [NSString stringWithFormat:@"上次同步时间 %d月%d日 %d:%d",month,day,hour,min];
    dateLabel.textColor = [UIColor whiteColor];
    dateLabel.textAlignment = NSTextAlignmentCenter;
    [self.gradeView addSubview:dateLabel];
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.gradeView.mas_bottom).offset(H(-20));
        make.width.equalTo(@(allWidth));
        make.height.equalTo(@15);
        make.centerX.equalTo(self.gradeView.mas_centerX);
    }];
    
    
    UILabel *moreThan = [[UILabel alloc] init];
    moreThan.font = [UIFont boldSystemFontOfSize:18.f];
    moreThan.text = @"恭喜！超过 90% 的人";
    moreThan.textColor = [UIColor whiteColor];
    moreThan.textAlignment = NSTextAlignmentCenter;
    [self.gradeView addSubview:moreThan];
    [moreThan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(dateLabel.mas_top).offset(-10);
        make.width.equalTo(@(allWidth));
        make.height.equalTo(@18);
        make.centerX.equalTo(self.gradeView.mas_centerX);
    }];
    
    
    
    
    /* 弧度 */
    CGFloat width = self.view.frame.size.width;
    CAShapeLayer *radian = [CAShapeLayer layer];
    /* 验证码线 */
    radian.frame = CGRectMake(0, gradViewHeight, width, 0);
    radian.lineWidth = 1;                            // 线宽
    radian.strokeColor = kGradeViewBackground.CGColor;       // 线的颜色
    radian.fillColor = [UIColor colorWithHex:0xB1B2C9].CGColor;         // 填充色
    
    UIBezierPath *healthpath = [UIBezierPath bezierPath];
    [healthpath moveToPoint:CGPointMake(0, 0)]; // 设置起点
    [healthpath addQuadCurveToPoint:CGPointMake(width, 0) controlPoint:CGPointMake(width / 2, H(60))]; // 设置终点和控制点
    radian.path = healthpath.CGPath;
    [self.scrollView.layer addSublayer:radian];
    
    
//    self.fan = [CAShapeLayer layer];
//    self.fan.frame = CGRectMake(0, 64, width, 0);
//    self.fan.lineWidth = 1;
//    self.fan.strokeColor = kGradeViewBackground.CGColor;
//    self.fan.fillColor = kGradeViewBackground.CGColor;
//    
//    self.fan.path = healthpath.CGPath;
//    [self.view.layer addSublayer:self.fan];
//    [self.fan setHidden:YES];
    
    
    
    UILabel *myHealth = [[UILabel alloc] init];
    myHealth.text = @"我的生理";
    myHealth.textColor = [UIColor blackColor];
    myHealth.font = [UIFont boldSystemFontOfSize:15.f];
    //    myHealth.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:myHealth];
    [myHealth mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.gradeView.mas_bottom).offset(labelTopPadding * 2);
        make.left.equalTo(self.scrollView.mas_left).offset(AllLeftPadding);
        make.width.equalTo(@(allWidth));
        make.height.equalTo(@(labelHeight));
    }];
    
    /* 生理模块 - 月经 */
    self.physiologyView = [[UIView alloc] init];
    self.physiologyView.backgroundColor = [UIColor colorWithHex:0xB0B2C9 alpha:1];
    self.physiologyView.layer.cornerRadius = 15;
    [self.scrollView addSubview:self.physiologyView];
    [self.physiologyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(myHealth.mas_bottom).offset(viewTopPadding);
        make.width.equalTo(@(allWidth));
        make.height.equalTo(@(physiologyViewHeight));
        make.centerX.equalTo(self.scrollView.mas_centerX);
    }];
    
    UILabel *menstrualStart = [[UILabel alloc] init];
    menstrualStart.text = @"月经期 第2天";
    menstrualStart.font = [UIFont boldSystemFontOfSize:18.f];
    menstrualStart.textColor = [UIColor whiteColor];
    [self.physiologyView addSubview:menstrualStart];
    [menstrualStart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.physiologyView.mas_centerY).offset(-5);
        make.left.equalTo(@20);
        make.height.equalTo(@18);
    }];
    
    UILabel *menstrualEnd = [[UILabel alloc] init];
    menstrualEnd.text = @"预计经期结束还有 4天";
    menstrualEnd.font = [UIFont systemFontOfSize:12.f];
    menstrualEnd.textColor = [UIColor whiteColor];
    [self.physiologyView addSubview:menstrualEnd];
    [menstrualEnd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.physiologyView.mas_centerY).offset(5);
        make.left.equalTo(@20);
        make.height.equalTo(@12);
    }];
    
    self.dateButton = [[UIButton alloc] init];
    self.dateButton.backgroundColor = [UIColor whiteColor];
    [self.dateButton addTarget:self action:@selector(gotoCalendar) forControlEvents:UIControlEventTouchUpInside];
    [self.physiologyView addSubview:self.dateButton];
    [self.dateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.physiologyView.mas_centerY);
        make.right.equalTo(self.physiologyView.mas_right).offset(-20);
        make.width.height.equalTo(@(H(50)));
    }];
    
    
    
    
    /* 生理模块 - 健康指标 */
    self.healthView = [[UIView alloc] init];
    self.healthView.backgroundColor = [UIColor whiteColor];
    self.healthView.layer.cornerRadius = 15;
    [self.scrollView addSubview:self.healthView];
    [self.healthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.physiologyView.mas_bottom).offset(viewTopPadding);
        make.left.equalTo(self.physiologyView.mas_left);
        make.right.equalTo(self.physiologyView.mas_right);
        make.height.equalTo(@(healthViewHeight));
    }];
    
    
    
    self.healthButton = [[UIButton alloc] init];
    CGFloat score_physiology = [self.userClient.userStateInfo[@"score_physiology"] floatValue];
    if (score_physiology >= 115) {
        [self.healthButton setTitle:@"你的生理指标正常，点击查看详情 " forState:UIControlStateNormal];
    }else if (score_physiology < 115) {
        [self.healthButton setTitle:@"你的生理指标异常，点击查看详情 " forState:UIControlStateNormal];
    }
    
    [self.healthButton setTitleColor:[UIColor colorWithHex:0xBEBCBC alpha:1] forState:UIControlStateNormal];
    self.healthButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    //    [self.lustButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    self.healthButton.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [self.healthButton setImage:[UIImage imageNamed:@"进入"] forState:UIControlStateNormal];
    //    self.lustButton.imageView.frame.size = CGPointMake(13, 13);
    
    [self.healthButton addTarget:self action:@selector(gotoHealthDetails) forControlEvents:UIControlEventTouchUpInside];
    
    // 图片右移
    self.healthButton.imageEdgeInsets = UIEdgeInsetsMake(0, W(230), 0, 0);
    // 文字左移
    self.healthButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, W(0));
    
    [self.healthView addSubview:self.healthButton];
    [self.healthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.healthView.mas_bottom).offset(-20);
        make.right.equalTo(self.healthView.mas_right).offset(-20);
        make.width.equalTo(@W(240));
        make.height.equalTo(@13);
    }];
    
    CGFloat pregnancy = [self.userClient.userStateInfo[@"pregnancy"] floatValue]; // 怀孕几率
    CGFloat dysmenorrhea = [self.userClient.userStateInfo[@"dysmenorrhea"] floatValue]; // 痛经程度
    
    [self setHealthIndex:self.healthView titleLabel:@"怀孕几率" percent:pregnancy percentLabel:[self.userClient.userStateInfo[@"pregnancy"] stringByAppendingFormat:@"%%"] index:0];
    
    [self setHealthIndex:self.healthView titleLabel:@"痛经程度" percent:dysmenorrhea percentLabel:[self.userClient.userStateInfo[@"dysmenorrhea"] stringByAppendingFormat:@"%%"] index:1];
    
    [self setHealthIndex:self.healthView titleLabel:@"体质状况" percent:100 percentLabel:[self.userClient.userStateInfo[@"physique_status"] intValue] == 0 ? @"健康" : @"宫寒" index:2];
    
    
    
    UILabel *lustLabel = [[UILabel alloc] init];
    lustLabel.font = [UIFont boldSystemFontOfSize:15.f];
    lustLabel.text = @"我的情欲";
    myHealth.textColor = [UIColor blackColor];
    [self.scrollView addSubview:lustLabel];
    [lustLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.healthView.mas_bottom).offset(labelTopPadding);
        make.left.equalTo(self.scrollView.mas_left).offset(AllLeftPadding);
        make.width.equalTo(@(allWidth));
        make.height.equalTo(@(labelHeight));
    }];
    
    /* 情欲模块 */
    
    self.lustView = [[UIView alloc] init];
    self.lustView.backgroundColor = [UIColor whiteColor];
    self.lustView.layer.cornerRadius = 15;
    [self.scrollView addSubview:self.lustView];
    [self.lustView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lustLabel.mas_bottom).offset(viewTopPadding);
        make.left.equalTo(self.healthView.mas_left);
        make.right.equalTo(self.healthView.mas_right);
        make.height.equalTo(@(lustViewHeight));
    }];
    
    CGFloat score_lust = [self.userClient.userStateInfo[@"score_erotic"] floatValue];
    CGFloat love_times = [self.userClient.userStateInfo[@"love_times"] floatValue];
    CGFloat burn_calories = [self.userClient.userStateInfo[@"burn_calories"] floatValue];
    CGFloat climax = [self.userClient.userStateInfo[@"climax"] floatValue] / love_times *100;
    
    
    /* 设置情欲模块的3个圆圈+title+数值 */
    [self setHealthIndex:self.lustView titleLabel:@"爱爱" percent:love_times > 0 ? 100 : 0 percentLabel:[NSString stringWithFormat:@"%@次",self.userClient.userStateInfo[@"love_times"]] index:0];
    
    [self setHealthIndex:self.lustView titleLabel:@"卡路里" percent:burn_calories > 0 ? 100 : 0 percentLabel:[NSString stringWithFormat:@"%@千卡",self.userClient.userStateInfo[@"burn_calories"]] index:1];
    
    [self setHealthIndex:self.lustView titleLabel:@"高潮" percent:climax percentLabel:[NSString stringWithFormat:@"%@次",self.userClient.userStateInfo[@"climax"]] index:2];
    
    
    
    UILabel *lustContentLabel = [[UILabel alloc] init];
    if (love_times > 2 && love_times < 10 && score_lust >= 110) {
        lustContentLabel.text = @"Bomb猛暴型,静如处子动如惊鸿，你控制的很好";
    }else if (love_times >= 10 && score_lust >= 110) {
        lustContentLabel.text = @"Agitation激动型,传说中的性瘾都快望尘莫及啦/就是这样强烈到不能自控";
    }else if (love_times >= 2 && score_lust < 110) {
        lustContentLabel.text = @"Distressed苦恼型,性爱要追求质量噢";
    }else if (love_times <= 2) {
        lustContentLabel.text = @"Cool冷漠型,你这么冷你知道么/传说中的性冷淡也跟不上你啦";
    }
    lustContentLabel.font = [UIFont systemFontOfSize:13.0f];
    lustContentLabel.textAlignment = NSTextAlignmentCenter;
    [self.lustView addSubview:lustContentLabel];
    [lustContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lustView.mas_top).offset(H(150));
        make.left.equalTo(self.healthView.mas_left);
        make.right.equalTo(self.healthView.mas_right);
        make.height.equalTo(@13);
    }];
    
    
    self.lustButton = [[UIButton alloc] init];
    [self.lustButton setTitle:@"你的情欲表现出色，点击查看详情 " forState:UIControlStateNormal];
    [self.lustButton setTitleColor:[UIColor colorWithHex:0xBEBCBC alpha:1] forState:UIControlStateNormal];
    self.lustButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    
    self.lustButton.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [self.lustButton setImage:[UIImage imageNamed:@"进入"] forState:UIControlStateNormal];
    
    [self.lustButton addTarget:self action:@selector(gotoLustDetails) forControlEvents:UIControlEventTouchUpInside];
    
    // 图片右移
    self.lustButton.imageEdgeInsets = UIEdgeInsetsMake(0, W(230), 0, 0);
    // 文字左移
    self.lustButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, W(0));
    
    [self.lustView addSubview:self.lustButton];
    [self.lustButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lustView.mas_bottom).offset(-20);
        make.right.equalTo(self.lustView.mas_right).offset(-20);
        make.width.equalTo(@W(240));
        make.height.equalTo(@13);
    }];
    
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = @"已经到底了，别扯了";
    bottomLabel.textColor = [UIColor grayColor];
    bottomLabel.font = [UIFont systemFontOfSize:10.f];
    [self.scrollView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.scrollView.mas_centerX);
        make.centerY.equalTo(self.lustView.mas_bottom).offset(H(25));
        make.height.equalTo(@10);
    }];
    
    UILabel *bottomLeft = [[UILabel alloc] init];
    bottomLeft.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:bottomLeft];
    [bottomLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomLabel.mas_centerY);
        make.right.equalTo(bottomLabel.mas_left).offset(-20);
        make.height.equalTo(@1);
        make.width.equalTo(@50);
    }];
    
    UILabel *bottomRight = [[UILabel alloc] init];
    bottomRight.backgroundColor = [UIColor lightGrayColor];
    [self.scrollView addSubview:bottomRight];
    [bottomRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomLabel.mas_centerY);
        make.left.equalTo(bottomLabel.mas_right).offset(20);
        make.height.equalTo(@1);
        make.width.equalTo(@50);
    }];
    
    
    //    UIButton *top_image = [[UIButton alloc] init];
    //    top_image
    
    
    
    
}

- (void)gotoCalendar {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"State" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [story instantiateViewControllerWithIdentifier:@"CalendarViewController"];
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}


/**
 跳转至生理详情页
 */
- (void)gotoHealthDetails {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"State" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [story instantiateViewControllerWithIdentifier:@"HealthDetailsViewController"];
    
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}


/**
 跳转至情欲详情页
 */
- (void)gotoLustDetails {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"State" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [story instantiateViewControllerWithIdentifier:@"LustDetailsViewController"];
    //    LustDetailsViewController *lustViewController = [[LustDetailsViewController alloc] init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}

- (CGPathRef)getBezierLine:(CGFloat)percentage {
    
    /* 设置半径为10 */
    CGFloat radius = W(80);
    CGFloat pathX = self.view.centerX;
    CGFloat pathY = 10 + radius + H(200);
    if (percentage >= 100) {
        percentage = 3.5f;
    }else if (percentage >= 75) {
        percentage = (percentage - 75) / 5 * 0.1 + 1;
    }else if (percentage >= 50) {
        percentage = (percentage - 50) / 5 * 0.1 + 0.5;
    }else if (percentage >= 25) {
        percentage = (percentage - 25) / 5 * 0.1 + 0;
    }else if (percentage >= 0) {
        percentage = percentage / 5 * 0.1 + 1.5;
    }
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(pathX, pathY) radius:radius startAngle:1.5*M_PI endAngle:percentage*M_PI clockwise:YES];
    
    return path.CGPath;
}

- (void)startAnimation {
    /* 半圆动画开始出现 */
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    
    animation.fromValue = [NSNumber numberWithFloat:0];
    animation.toValue = [NSNumber numberWithFloat:1];
    
    CAAnimationGroup *strokeAnimationGroup = [CAAnimationGroup animation];
    strokeAnimationGroup.duration = 1;
    strokeAnimationGroup.animations = @[animation];
    self.bigRoundLine.path = [self getBezierLine:88];
    [self.bigRoundLine addAnimation:strokeAnimationGroup forKey:nil];
}

/**
 设置内容
 
 @param view 模块
 @param title 标题
 @param percent 百分数
 @param percentStr 分数的值
 @param i 第几个
 */
- (void)setHealthIndex:(UIView *)view titleLabel:(NSString *)title percent:(CGFloat)percent percentLabel:(NSString *)percentStr index:(int)i {
    
    CGFloat titleTopPadding = 30;
    
    CGFloat labelWidth = (self.view.frame.size.width - 30) / 3;   // 标题的宽度
    CGFloat leftPadding = 0;  // 左边边距
    CGFloat topPadding = 15.f;   // 顶部边距
    CGFloat labelHeight = 15.f;  // label 的宽度
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(leftPadding + (labelWidth + leftPadding) * i, titleTopPadding, labelWidth, labelHeight);
    label.text = title;
    label.font = [UIFont systemFontOfSize:15.0f];
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    /* 半圆的线 */
    CAShapeLayer *layer = [CAShapeLayer new];
    CAShapeLayer *layerBack = [CAShapeLayer new];
    /* 线宽 */
    layer.lineWidth = 3;
    layerBack.lineWidth = 3;
    /* 圆环边缘是圆的 */
    layer.lineCap = kCALineCapRound;
    /* 圆环的颜色 */
    layer.strokeColor = [UIColor colorWithHex:0xB0B2C9 alpha:1].CGColor;
    layerBack.strokeColor = [UIColor colorWithHex:0xF6F6FA alpha:1].CGColor;
    /* 背景填充色 */
    layer.fillColor = [UIColor clearColor].CGColor;
    layerBack.fillColor = [UIColor clearColor].CGColor;
    /* 设置半径为10 */
    CGFloat radius = labelWidth / 3;
    /* 按照顺时针方向 */
    BOOL clockWise = true;
    
    CGFloat pathX = label.frame.origin.x + labelWidth / 2;
    CGFloat pathY = titleTopPadding + labelHeight + topPadding + radius;
    if (percent >= 100) {
        percent = 3.5f;
    }else if (percent >= 75) {
        percent = (percent - 75) / 5 * 0.1 + 1;
    }else if (percent >= 50) {
        percent = (percent - 50) / 5 * 0.1 + 0.5;
    }else if (percent >= 25) {
        percent = (percent - 25) / 5 * 0.1 + 0;
    }else if (percent >= 0) {
        percent = percent / 5 * 0.1 + 1.5;
    }
    /* 初始化一个路径 */
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(pathX, pathY) radius:radius startAngle:1.5*M_PI endAngle:percent*M_PI clockwise:clockWise];
    layer.path = [path CGPath];
    
    /* 整圆半径 */
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(pathX, pathY) radius:radius startAngle:1.5*M_PI endAngle:3.5*M_PI clockwise:clockWise];
    layerBack.path = [path1 CGPath];
    
    [view.layer addSublayer:layerBack];
    [view.layer addSublayer:layer];
    
    CGFloat font = 15.f;
    CGFloat top = topPadding + radius;
    UILabel *label2 = [[UILabel alloc] init];
    label2.font = [UIFont boldSystemFontOfSize:font];
    label2.textColor = [UIColor colorWithHex:0xB0B2C9 alpha:1];
    label2.text = percentStr;
    label2.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(label.mas_centerX);
        make.centerY.equalTo(label.mas_bottom).offset(top);
        make.height.equalTo(@(font));
        make.width.equalTo(@(radius * 2));
    }];
}


/**
 添加Nav左边按钮
 */
-(void)setCenterButton{
    self.left_menu = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.left_menu setFrame:CGRectMake(0, 0, 40, 40)];
    [self.left_menu addTarget:self action:@selector(showUserCenter:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*userHeadItem = [[UIBarButtonItem alloc] initWithCustomView:self.left_menu];
    [self.navigationItem setLeftBarButtonItem:userHeadItem animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)bindViewModel{
    [super bindViewModel];
    @weakify(self)
    [RACObserve(self.viewModel, userStateInfo) subscribeNext:^(id x) {
        @strongify(self)
        NSDictionary * info =self.viewModel.userStateInfo;
        //        [self.image.imageView setImageWithURLString:[[info allKeys] containsObject:@"img_top"]?info[@"img_top"]: info[@"portrait"]  placeholderImage:nil];
        
        //        self.left_menu
        [self.left_menu setBackgroundImageUrlForState:UIControlStateNormal withUrl:[[info allKeys] containsObject:@"img_top"]?info[@"img_top"]: info[@"portrait"] placeholderImage:nil];
        self.left_menu.layer.cornerRadius = 20;
        self.left_menu.layer.masksToBounds = YES;
    }];
}
#pragma mark -- getter and setter

- (StateViewModel *)viewModel{
    if (_viewModel==nil) {
        _viewModel = [[StateViewModel alloc] initWithViewController:self];
    }
    return _viewModel;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%f %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
    
    //    /* 正着滑 */
    //    if (self.oldContentOffset < scrollView.contentOffset.y) {
    //        self.oldContentOffset = scrollView.contentOffset.y;
    //        if (scrollView.contentOffset.y >= 310) {
    //            [self.fan setHidden:NO];
    //            //        [self.left_menu setHidden:YES];
    //            if (self.newWidth < self.view.width / 2 - 20 && self.newHeight < H(23)) {
    //                self.newWidth = self.newWidth + W(3.63);
    //                self.newHeight = self.newHeight + H(0.46);
    //            }
    //
    //
    //
    //            [self.left_menu setFrame:CGRectMake(self.newWidth, self.newHeight, 40, 40)];
    //        }
    //    }else if (self.oldContentOffset > scrollView.contentOffset.y) {
    //        self.oldContentOffset = scrollView.contentOffset.y;
    //        if (scrollView.contentOffset.y < 310) {
    //
    //            if (self.newWidth > 6 && self.newHeight >2) {
    //                self.newWidth = self.newWidth - W(3.63);
    //                self.newHeight = self.newHeight - H(0.46);
    //            }
    //            [self.fan setHidden:YES];
    //            //        [self.left_menu setHidden:NO];
    //            [self.left_menu setFrame:CGRectMake( self.newWidth, self.newHeight, 40, 40)];
    //        }
    //    }
    
    /* 往回滑 */
    //
//    if (scrollView.contentOffset.y >= 310) {
//        [self.fan setHidden:NO];
//        //        [self.left_menu setHidden:YES];
//        if (self.newWidth < self.view.width / 2 - 20 && self.newHeight < H(23)) {
//            self.newWidth = self.newWidth + W(6.46);
//            self.newHeight = self.newHeight + H(0.46);
//        }
//        
//        self.left_menu.cornerRadius = 25;
//        
//        [self.left_menu setFrame:CGRectMake(self.view.width / 2 - 20, 20, 50, 50)];
//    }else {
//        [self.fan setHidden:YES];
//        //        [self.left_menu setHidden:NO];
//        self.left_menu.cornerRadius = 20;
//        [self.left_menu setFrame:CGRectMake( 6, 2, 40, 40)];
//    }
    
    
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
